package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import officeserver.log_error.UserException;
import officeserver.office.Office;
import officeserver.users.AccessLevel;

import GUI.main.MainClient;
import GUI.main.navigation.NavigationMenuPanel;
import GUI.network.OfficeConnection;

/**
 * @author mramsey3
 * 
 */
public class AddPatientDialog extends JDialog implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    // panels...
    private JPanel titlePanel;
    private JPanel centerPanel;
    private JPanel firstNamePanel;
    private JPanel lastNamePanel;
    private JPanel usernamePanel;
    private JPanel passwordPanel;
    private JPanel emailPanel;
    private JPanel phonePanel;
    private JPanel agePanel;
    private JPanel buttonPanel;

    // labels...
    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JLabel emailLabel;
    private JLabel phoneLabel;
    private JLabel ageLabel;

    // text fields...
    private JTextField firstNameField;
    private JTextField lastNameField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField emailField;
    private JTextField phoneField;
    private JTextField ageField;

    // buttons...
    private JButton createButton;
    private JButton cancelButton;

    private final static int WIDTH = 400;
    private final static int HEIGHT = 370;

    private NavigationMenuPanel navigationMenuPanel;

    /**
     * 
     */
    public AddPatientDialog(NavigationMenuPanel navigationMenuPanel) {
	this.navigationMenuPanel = navigationMenuPanel;
	setSize(new Dimension(WIDTH, HEIGHT));
	setLocation(50, 50);
	setLayout(new BorderLayout());

	add(getTitlePanel(), BorderLayout.NORTH);
	add(getCenterPanel(), BorderLayout.CENTER);
	add(getButtonPanel(), BorderLayout.SOUTH);
    }

    /**
     * @return
     */
    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(new Color(100, 150, 220));
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("add patient:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(WIDTH, 50));
	}
	return titlePanel;
    }

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getFirstNamePanel());
	    centerPanel.add(getLastNamePanel());
	    centerPanel.add(getUsernamePanel());
	    centerPanel.add(getPasswordPanel());
	    centerPanel.add(getEmailPanel());
	    centerPanel.add(getPhonePanel());
	    centerPanel.add(getAgePanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getFirstNamePanel() {
	if (firstNamePanel == null) {
	    firstNamePanel = new JPanel();

	    firstNamePanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getFirstNameLabel(), gbcA);
	    firstNamePanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getFirstNameField(), gbcB);
	    firstNamePanel.add(rightPanel, BorderLayout.EAST);
	}
	return firstNamePanel;
    }

    /**
     * @return
     */
    private JLabel getFirstNameLabel() {
	if (firstNameLabel == null) {
	    firstNameLabel = new JLabel("    first name:");
	}
	return firstNameLabel;
    }

    /**
     * @return
     */
    private JTextField getFirstNameField() {
	if (firstNameField == null) {
	    firstNameField = new JTextField(20);
	    firstNameField.setText("first name");
	}
	return firstNameField;
    }

    /**
     * @return
     */
    private JPanel getLastNamePanel() {
	if (lastNamePanel == null) {
	    lastNamePanel = new JPanel();

	    lastNamePanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getLastNameLabel(), gbcA);
	    lastNamePanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getLastNameField(), gbcB);
	    lastNamePanel.add(rightPanel, BorderLayout.EAST);
	}
	return lastNamePanel;
    }

    /**
     * @return
     */
    private JLabel getLastNameLabel() {
	if (lastNameLabel == null) {
	    lastNameLabel = new JLabel("    last name:");
	}
	return lastNameLabel;
    }

    /**
     * @return
     */
    private JTextField getLastNameField() {
	if (lastNameField == null) {
	    lastNameField = new JTextField(20);
	    lastNameField.setText("last name");
	}
	return lastNameField;
    }

    /**
     * @return
     */
    private JPanel getUsernamePanel() {
	if (usernamePanel == null) {
	    usernamePanel = new JPanel();

	    usernamePanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getUsernameLabel(), gbcA);
	    usernamePanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getUsernameField(), gbcB);
	    usernamePanel.add(rightPanel, BorderLayout.EAST);
	}
	return usernamePanel;
    }

    /**
     * @return
     */
    private JLabel getUsernameLabel() {
	if (usernameLabel == null) {
	    usernameLabel = new JLabel("    username:");
	}
	return usernameLabel;
    }

    /**
     * @return
     */
    private JTextField getUsernameField() {
	if (usernameField == null) {
	    usernameField = new JTextField(20);
	    usernameField.setText("username");
	}
	return usernameField;
    }

    /**
     * @return
     */
    private JPanel getPasswordPanel() {
	if (passwordPanel == null) {
	    passwordPanel = new JPanel();

	    passwordPanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getPasswordLabel(), gbcA);
	    passwordPanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getPasswordField(), gbcB);
	    passwordPanel.add(rightPanel, BorderLayout.EAST);
	}
	return passwordPanel;
    }

    /**
     * @return
     */
    private JLabel getPasswordLabel() {
	if (passwordLabel == null) {
	    passwordLabel = new JLabel("    password:");
	}
	return passwordLabel;
    }

    /**
     * @return
     */
    private JPasswordField getPasswordField() {
	if (passwordField == null) {
	    passwordField = new JPasswordField(20);
	    passwordField.setText("password");
	}
	return passwordField;
    }

    /**
     * @return
     */
    private JPanel getEmailPanel() {
	if (emailPanel == null) {
	    emailPanel = new JPanel();

	    emailPanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getEmailLabel(), gbcA);
	    emailPanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getEmailField(), gbcB);
	    emailPanel.add(rightPanel, BorderLayout.EAST);
	}
	return emailPanel;
    }

    /**
     * @return
     */
    private JLabel getEmailLabel() {
	if (emailLabel == null) {
	    emailLabel = new JLabel("    email:");
	}
	return emailLabel;
    }

    /**
     * @return
     */
    private JTextField getEmailField() {
	if (emailField == null) {
	    emailField = new JTextField(20);
	    emailField.setText("email@email.com");
	}
	return emailField;
    }

    /**
     * @return
     */
    private JPanel getPhonePanel() {
	if (phonePanel == null) {
	    phonePanel = new JPanel();

	    phonePanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getPhoneLabel(), gbcA);
	    phonePanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getPhoneField(), gbcB);
	    phonePanel.add(rightPanel, BorderLayout.EAST);
	}
	return phonePanel;
    }

    /**
     * @return
     */
    private JLabel getPhoneLabel() {
	if (phoneLabel == null) {
	    phoneLabel = new JLabel("    phone number:");
	}
	return phoneLabel;
    }

    /**
     * @return
     */
    private JTextField getPhoneField() {
	if (phoneField == null) {
	    phoneField = new JTextField(20);
	    phoneField.setText("phone");
	}
	return phoneField;
    }

    /**
     * @return
     */
    private JPanel getAgePanel() {
	if (agePanel == null) {
	    agePanel = new JPanel();

	    agePanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.EAST;
	    gbcA.weightx = 1;
	    leftPanel.add(getAgeLabel(), gbcA);
	    agePanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.WEST;
	    gbcB.weightx = 1;
	    rightPanel.add(getAgeField(), gbcB);
	    agePanel.add(rightPanel, BorderLayout.EAST);
	}
	return agePanel;
    }

    /**
     * @return
     */
    private JLabel getAgeLabel() {
	if (ageLabel == null) {
	    ageLabel = new JLabel("    age:");
	}
	return ageLabel;
    }

    /**
     * @return
     */
    private JTextField getAgeField() {
	if (ageField == null) {
	    ageField = new JTextField(20);
	    ageField.setText("age");
	}
	return ageField;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setPreferredSize(new Dimension(WIDTH, 35));
	    buttonPanel.add(getCreateButton());
	    buttonPanel.add(getCancelButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private JButton getCreateButton() {
	if (createButton == null) {
	    createButton = new JButton("create");
	    createButton.addActionListener(this);
	}
	return createButton;
    }

    /**
     * @return
     */
    private JButton getCancelButton() {
	if (cancelButton == null) {
	    cancelButton = new JButton("cancel");
	    cancelButton.addActionListener(this);
	}
	return cancelButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == createButton) {
	    OfficeConnection temp = null;
	    temp = MainClient.getConnection();
	    try {
		temp.addUser(getFirstNameField().getText(), getLastNameField()
			.getText(), getUsernameField().getText(), String
			.valueOf(getPasswordField().getPassword()),
			AccessLevel.PATIENT);
		if (MainClient.getConnection().getSelfPersonalInfo()
			.getAccessLevel() == AccessLevel.NURSE) {
		    navigationMenuPanel.getNurseNavigationMenuPanel()
			    .refreshPanel();
		} else {
		    navigationMenuPanel.getDoctorNavigationMenuPanel()
			    .refreshPanel();
		}
		this.setVisible(false);

	    } catch (UserException e1) {
		e1.printStackTrace();
	    } /*
	       * catch (IOException e1) { e1.printStackTrace(); } catch
	       * (ClassNotFoundException e1) { e1.printStackTrace(); }
	       */
		catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (ClassNotFoundException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	} else if (e.getSource() == cancelButton) {
	    setVisible(false);
	}

    }

}
